package com.vita.core2;

import java.util.Random;
import java.util.TreeSet;
import java.util.concurrent.CompletableFuture;

/**
 * 电梯
 */
public class Machine {
    private int id;

    /**
     * 运行状态：0静止、1向上、-1向下
     */
    private int state = 0;

    /**
     * 当前所在楼层
     */
    private int nowFloor = 1;

    private Central central;

    /**
     * 内部列表，升序
     */
    private TreeSet<Integer> innerList = new TreeSet<>(Integer::compareTo);

    public Machine(int id) {
        this.id = id;
    }

    public TreeSet<Integer> getInnerList() {
        return innerList;
    }

    public void setInnerList(TreeSet<Integer> innerList) {
        this.innerList = innerList;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public int getNowFloor() {
        return nowFloor;
    }

    public void setNowFloor(int nowFloor) {
        this.nowFloor = nowFloor;
    }

    /**
     * 返回本层是否已经开门
     */
    private boolean openTheDoor(){
        if(this.state == 1){
            if(Helper.isOpen(this.innerList,this.central.getUpList(),this.nowFloor)){
                return true;
            }
        }else if(this.state == -1) {
            if(Helper.isOpen(this.innerList,this.central.getDwList(),this.nowFloor)){
                return true;
            }
        }
        return false;
    }

    private void upDw() throws InterruptedException {
        // 开门,并清空本层同向
        if(openTheDoor()){
            System.out.println("电梯 " + this.id + " 号，" + this.nowFloor + "层，开门------");
            Thread.sleep(1500);
        }

        // 如果静止，判断电梯需要上升、下降（根据最短距离判断）
        if(this.state == 0){
            this.state = Helper.isUpOrDw(this, central);
        }

        // 非静止，判断是否继续上升、下降（根据是否还有目标值判断）
        if(Helper.isContinue(this,this.central)){
            if(openTheDoor()){
                System.out.println("电梯 " + this.id + " 号，" + this.nowFloor + "层，开门------");
                Thread.sleep(1500);
            }

            if(this.state == 1){
                System.out.println("电梯 " + this.id + " 号，" + this.nowFloor + "层，上行");
            }else if (this.state == -1){
                System.out.println("电梯 " + this.id + " 号，" + this.nowFloor + "层，下行");
            }
            this.nowFloor = this.nowFloor + getState();
            Thread.sleep(1500);
            upDw();
            return;
        }else {
            System.out.println("电梯 " + this.id + " 号，停在" + this.nowFloor + "层");
            return;
        }
    }

    public void init(Central central) {
        System.out.println("电梯 " + id + " 号，启动-------------");
        this.state = 0;
        this.innerList.clear();
        this.central = central;
        // int number = new Random().nextInt(10) + 1;
        // this.innerList.add(3);
        // this.innerList.add(number);
    }

    public CompletableFuture<Integer> run(){
        return CompletableFuture.supplyAsync(() -> {
            try {
                upDw();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        });
    }
}
